<?php
// src/Pyaf/GymBundle/Repository/AlumnoRepository.php
namespace Pyaf\GymBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * Description of AlumnoRepository
 *
 * @author Toshiba P755-S5272
 */
class AlumnoRepository extends EntityRepository{
    
    /**
     *  
     * @return a list of Profesores order by name
     */
    public function findAllOrderByNombre() {        
        return $this->getEntityManager()
                ->createQuery('SELECT a FROM PyafGymBundle:Alumno a ORDER BY a.id ASC')                
                ->getResult();
    }
    
    public function findEnablesOrderByNombre(){
        $em = $this->getEntityManager();
        $qb = $em->createQueryBuilder();

        $qb->select(array('a', 'p'))
           ->from('PyafGymBundle:Alumno', 'a')
           ->leftJoin('a.persona', 'p')
           ->where("p.habilitado = true");
        $query = $qb->getQuery();
        $results = $query->getResult();

        return $results;
    }
    
    public function findAlumno($id) {
        return $this->getEntityManager()
                ->createQuery("SELECT a FROM PyafGymBundle:Alumno a WHERE a.id = $id")
                ->getOneOrNullResult();
    }
    
    public function findUserDni($dni){
        $em = $this->getEntityManager();
        $qb = $em->createQueryBuilder();
        
        $qb->select(array('p'))
           ->from('PyafGymBundle:Persona', 'p')
           ->where("p.dni = $dni");
        $query = $qb->getQuery();
        $results = $query->getResult();

        return $results;
    }
    
    public function save($alumno){
        $this->saveAlumno($alumno);
    }
    
    public function saveAlumno($alumno) {
        $em = $this->getEntityManager();
        $em->persist($alumno);
        $em->flush();
    }
}

?>
